﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using PathologicalGames;

namespace GameJam
{
	public class BulletSpawner : MonoBehaviour 
	{
		[SerializeField]
		private GameObject _prefab;
		[SerializeField]
		public string poolName = "Bullet";
		SpawnPool shapesPool;

		[SerializeField]
		float _interval = 0.2f;
		float _timer = 0.0f;
		bool _bCouldShot;

		[SerializeField]
		TreePlayer _owner;
		public TreePlayer Owner 
		{
			get{ return _owner;}
			set{ _owner = value;}
		}

		// Use this for initialization
		void Start ()
		{	
			SpawnPool shapesPool = PoolManager.Pools [this.poolName];
			if (shapesPool == null)
				shapesPool = PoolManager.Pools.Create (this.poolName);
		}
	
		// Update is called once per frame
		void Update ()
		{
			if (!_bCouldShot)
				_timer += Time.deltaTime;
			if (_timer > _interval) {
				_timer = 0.0f;
				_bCouldShot = true;
			}
		}

		public void ShootBullet(Quaternion quat,Color color,float speed)
		{
			if (!_bCouldShot)
				return;
		
			shapesPool = PoolManager.Pools [this.poolName];
			Transform prefab_trans = shapesPool.Spawn (_prefab);
			prefab_trans.position = this.transform.position;
			Bullet bullet = prefab_trans.GetComponent<Bullet> ();
			bullet.transform.rotation = quat;
			bullet.Owner = _owner;
            bullet.MoveSpeed = speed;
			bullet.SetColor (color);

            _bCouldShot = false;
		}
	}
}
